package cn.jcby.app.lebu.controller;


import cn.jcby.app.lebu.controller.req.BasicListReq;
import cn.jcby.app.lebu.entity.UserEnergyEntity;
import cn.jcby.app.lebu.service.UserEnergyService;
import cn.jcby.app.lebu.utils.PageUtils;
import cn.jcby.app.lebu.utils.ParamUtils;
import cn.jcby.app.lebu.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;



/**
 * 用户能量值日志
 *
 * @author lht
 * @email 
 * @date 2023-12-07 15:13:35
 */
@RestController
@RequestMapping("/user/energy")
@Api(tags="用户能量值日志")
public class UserEnergyController {
    @Autowired
    private UserEnergyService userEnergyService;

    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation("列表")
    @ApiResponses({
          @ApiResponse(code = 200,message = "ok",response = UserEnergyEntity.class)
    })
    public Result list(@RequestBody BasicListReq params){
        try {
            PageUtils page = userEnergyService.queryPage(params);
            return Result.ok().put("data", page);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage()) ;
        }
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("详情")
    @ApiResponses({
         @ApiResponse(code = 200,message = "ok",response = UserEnergyEntity.class)
    })
    public Result info(@PathVariable("id") Integer id){
        try {
            UserEnergyEntity userEnergy = userEnergyService.getById(id);
            return Result.ok().put("data", userEnergy);
        }catch (Exception e){
            e.printStackTrace();
             return Result.error(e.getMessage()) ;
        }
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("添加")
    public Result save(@RequestBody @Validated(Insert.class) UserEnergyEntity userEnergy,BindingResult bindingResult){
        try {
            ParamUtils.check(bindingResult);
            userEnergyService.add(userEnergy);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage()) ;
        }
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation("修改")
    public Result update(@RequestBody @Validated(Update.class) UserEnergyEntity userEnergy,BindingResult bindingResult){
        try {
          ParamUtils.check(bindingResult);
          userEnergyService.updateBy(userEnergy);
          return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage()) ;
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除")
    public Result delete(@RequestParam("id") Integer id){
        try {
            UserEnergyEntity userEnergy = userEnergyService.getById(id) ;
            if(userEnergy == null){
                return Result.error(id+"记录为空") ;
            }
            userEnergyService.removeById(id);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage()) ;
        }
    }

}
